////////////////////////////////////////////////////////////////////////////////////////
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING THIS SOFTWARE
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install, copy or use the software.
//
// Copyright (C) 2013, AerVision Technologies Pty Ltd, all rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of AerVision Technologies, may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors to demonstrate 
// www.AerCloud.net Framework as a Service API (FaaS), "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the copyright holder or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
////////////////////////////////////////////////////////////////////////////////////////

package com.aercloud.client.gui;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 * A dirty implementation for showing a resizing image only
 * 
 * @author rumen.dimov.mail@gmail.com
 *
 */
 public class LabelImage extends JLabel {
     
    ImageIcon imageIcon;
    
    public LabelImage()
    {
        super();
    }
    
    public LabelImage(ImageIcon paramIcon, Dimension dimension) {
        super();
        setMinimumSize(dimension);
        setPreferredSize(dimension);
        setMaximumSize(dimension);
        setIcon(paramIcon);
    }

    public LabelImage(Icon paramIcon) {
        super(paramIcon);
        // TODO Auto-generated constructor stub
    }

    public LabelImage(String paramString, Icon paramIcon, int paramInt) {
        super(paramString, paramIcon, paramInt);
        // TODO Auto-generated constructor stub
    }

    public LabelImage(String paramString, int paramInt) {
        super(paramString, paramInt);
        // TODO Auto-generated constructor stub
    }

    public LabelImage(String paramString) {
        super(paramString);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(this.imageIcon != null) {
            int imageWidth = imageIcon.getImage().getWidth(null);
            int imageHeight = imageIcon.getImage().getHeight(null);
//            if(imageWidth <= getWidth() && imageHeight <= getHeight()) {
//                g.drawImage(imageIcon.getImage(),0,getVerticalOffset(imageHeight),imageWidth,imageHeight, this);
//            } else
            if(imageWidth > getWidth() && imageHeight <= getHeight()) {
                double scale = (double)getWidth() / (double)imageWidth;
                imageHeight = (int)(scale * imageHeight);
                g.drawImage(imageIcon.getImage(),0,getVerticalOffset(imageHeight), (int)(scale * imageWidth), imageHeight, this);
            } else
            if(imageWidth <= getWidth() && imageHeight > getHeight()) {
                double scale = (double)getHeight() / (double)imageHeight;
                imageHeight = (int)(scale * imageHeight);
                g.drawImage(imageIcon.getImage(),0,getVerticalOffset(imageHeight), (int)(scale * imageWidth), imageHeight, this);
            } else {
                double scale = Math.min((double)getWidth() / (double)imageWidth, (double)getHeight() / (double)imageHeight);
                imageHeight = (int)(scale * imageHeight);
                g.drawImage(imageIcon.getImage(),0,getVerticalOffset(imageHeight), (int)(scale * imageWidth), imageHeight, this);
            }
        }
    }
    
    private int getVerticalOffset(int imageHeight) {
        if(SwingConstants.BOTTOM == getVerticalAlignment()) {
            return getHeight() - imageHeight;
        }
        return 0;
    }
    
    @Override
    public void setIcon(Icon icon) {
        this.imageIcon = (ImageIcon)icon;
        revalidate();
        repaint();
    }
}
